package com.atguigu.gulimall.search.thread;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.....start......");
        //异步对象
/*        CompletableFuture.runAsync(()->{
            System.out.println("当前线程："+Thread.currentThread().getId());
            System.out.println(5);
        },executor);*/

/*        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i=10/2;
            System.out.println(i);
            return i;
        }, executor).whenComplete((res,e)->{
            System.out.println("结果是："+res+"----异常是："+e);
        }).exceptionally(throwable -> {
            return  20;
        });*/

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println(i);
//            return i;
//        }, executor).handle((res, e) -> {
//            if (res != null) {
//                return res * 2;
//            }
//            if (e != null) {
//                return 0;
//            }
//            return 12;
//        });
        /**
         *  无法获取上一步结果，无返回值
         * .thenRunAsync(() -> {
         *             System.out.println("任务2启动");
         *         }, executor);
         *
         *
         * .thenAcceptAsync(res->{
         *             System.out.println("任务2启动了"+res);
         *         },executor);
         *         能接受上一步返回值，无返回值
         *
         * thenApplyAsync((res) -> {
         *             System.out.println("任务2启动了" + res);
         *             return "hello";
         *         }, executor);
         *          能接受上一步返回值，有返回值
         *
         */
/*        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
            return i;
        }, executor).thenApplyAsync((res) -> {
            System.out.println("任务2启动了" + res);
            return "hello";
        }, executor);


    }
  // 组合处理 thenCombine / thenAcceptBoth / runAfterBoth
          //applyToEither / acceptEither / runAfterEither

    //allOf / anyOf
    */

        CompletableFuture<String> future01= CompletableFuture.supplyAsync(() -> {
            System.out.println("线程1开始");
            return "a";
        }, executor);
        CompletableFuture<String> future02= CompletableFuture.supplyAsync(() -> {
            System.out.println("线程2开始");
            return "b";
        }, executor);

        CompletableFuture<String> future = future01.thenCombineAsync(future02, (r, u) -> {
            System.out.println("线程3开始" + r + u);
            return r + u;
        }, executor);
        System.out.println("main.....end......"+future.get());


    }
}